package com.wntms.wgj.web;

import com.github.pagehelper.PageInfo;
import com.wntms.gps.pojo.Result;
import com.wntms.wgj.pojo.CollectionSettle;
import com.wntms.wgj.pojo.FreightContracts;
import com.wntms.wgj.service.ChangesBusinessService;
import com.wntms.wgj.service.CollectionSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/changesBusiness")
public class ChangesBusinessController {
    @Autowired
    private CollectionSettleService collectionContractService;
    @Autowired
    private ChangesBusinessService changesBusinessService;
    /**
     * 查询所有业务变更信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<CollectionSettle> list = collectionContractService.findAll();
        Result<Object> result = new Result<>();
        if (list == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }

        return result;
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CollectionSettle> list = collectionContractService.findAll(currentPage,pageSize);

        Result<Object> result = new Result<>();
        if (list != null){
            PageInfo<CollectionSettle> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @PutMapping("/after")
    public Result upateBusiness(@RequestBody CollectionSettle collectionSettle){
        boolean flag = changesBusinessService.updateBusiness(collectionSettle);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("单挑变更失败");
        }else {
            result.setCode(200);
            result.setMsg("单条变更成功");
        }

        return result;
    }

    @PutMapping("/re")
    public Result updateRe(@RequestBody FreightContracts freightContracts){
        boolean flag = changesBusinessService.updateRe(freightContracts);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("单条变更失败");
        }else {
            result.setCode(200);
            result.setMsg("单条变更成功");
        }

        return result;
    }
}
